package com.xwaiy.xwaiyrpc.registry;

import com.xwaiy.xwaiyrpc.model.ServiceMetaInfo;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 注册中心缓存
 * @Author Xwaiy
 * @Date 2025/8/27 13:45
 **/
public class RegistryServiceCache {

    Map<String, List<ServiceMetaInfo>> serviceCache = new ConcurrentHashMap<>();

    /**
     * 写缓存
     * @param serviceKey 服务key
     * @param serviceCache 服务列表
     */
    void writeCache(String serviceKey, List<ServiceMetaInfo> serviceCache) {
        this.serviceCache.put(serviceKey, serviceCache);
    }

    /**
     * 读缓存
     * @param serviceKey 服务key
     * @return
     */
    List<ServiceMetaInfo> readCache(String serviceKey) {
        return this.serviceCache.get(serviceKey);
    }

    /**
     * 清空缓存
     */
    void cleanCache(){
        this.serviceCache.clear();
    }

    /**
     * 清空指定服务的缓存
     * @param serviceKey 服务key
     */
    void cleanCache(String serviceKey) {
        this.serviceCache.remove(serviceKey);
    }
}
